<?php

namespace app\activity\controller;

use library\Controller;


/** 活动
 * Created by PhpStorm.
 * User: ycp
 * Date: 2019/11/18
 * Time: 21:07
 */

class Package extends Controller
{
    protected $table = 'lite_activity_package';
    protected $activityTable = 'lite_activity';

    //活动首页
    public function index()
    {
        $this->activity_id = $this->request->param('activity_id');
        $activityName = db($this->activityTable)->where(['id' => $this->activity_id])->value('name');
        $this->title = $activityName. '套餐管理';
        $where = ['is_deleted' => '0', 'activity_id' =>  $this->activity_id];
        $this->statusTitle = ['0' => '未开始', '1' => '进行中', '2' => '已结束'];
        $this->_query($this->table)->like('name')->where($where)->page();
    }

    //添加活动
    public function add()
    {
        $this->activity_id = $this->request->param('activity_id');
        $this->title = '添加套餐';
        $this->isAddMode = '1';
        $this->room_type = config('data.room_type');
        $this->applyCsrfToken();
        return $this->_form($this->table, 'form');
    }

    public function edit()
    {
        $this->title = '编辑套餐';
        $this->activity_id = $this->request->param('activity_id');
        $this->isAddMode = '0';
        $this->room_type = config('data.room_type');
        $this->applyCsrfToken();
        return $this->_form($this->table, 'form');
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            $this->success('套餐编辑成功！', 'javascript:history.back()');
        }
    }

    public function del()
    {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }


    public function _form_filter(&$data)
    {
        if ($this->request->isGet()) {
            if(isset($data['end_time'])){
                $data['end_time'] = substr($data['end_time'], 0, 10);
            }
        } elseif ($this->request->isPost()) {
            $data['create_time'] = date('Y-m-d H:i:s');
        }
    }

}